unit PPS_Tambah;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection,Setting;

type
  TPPS_Form = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    desacombo: TComboBox;
    namainput: TEdit;
    jabatancombo: TComboBox;
    Label4: TLabel;
    tambahbtn: TButton;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    posisicombo: TComboBox;
    kecamatancombo: TComboBox;
    kabupatencombo: TComboBox;
    batalbtn: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure kabupatencomboChange(Sender: TObject);
    procedure kecamatancomboChange(Sender: TObject);
    procedure posisicomboChange(Sender: TObject);
    procedure tambahbtnClick(Sender: TObject);
    procedure desacomboChange(Sender: TObject);
    procedure batalbtnClick(Sender: TObject);
    function cekmasukan(posisi:integer):boolean;
  private
    { Private declarations }
  public
    { Public declarations }
    slogin,sdesa,sposisi,skota,skec,sjabatan:string;
    constructor Create(AOwner: TComponent; kode_login: String);             overload;
  end;

var
  PPS_Form: TPPS_Form;

implementation

{$R *.dfm}

constructor TPPS_Form.Create(AOwner: TComponent; kode_login: String);
begin
    slogin:=kode_login;
    inherited Create(AOwner);
end;
procedure TPPS_Form.batalbtnClick(Sender: TObject);
begin
  Close;
end;

procedure TPPS_Form.desacomboChange(Sender: TObject);
var
  desa:String;
  query:string;
  kode:string;
begin
  if (desacombo.ItemIndex > 0) then begin
    desa := desacombo.Items[desacombo.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(desa)+' AND KODE_KECAMATAN = '+QuotedStr(skec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sDesa := ZTable1.FieldByName('KODE_DESA').AsString;
  end
    else
  begin
    sDesa:='';
  end;
end;

procedure TPPS_Form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action := caFree;
end;

procedure TPPS_Form.FormCreate(Sender: TObject);
var setting:TSetting_Form;
kkota,kkec:string;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;

  BorderIcons := BorderIcons - [biMaximize];

  skota:='';
  sKec:='';
  sDesa:='';

  desacombo.items.Add('Pilih Kel/Desa');
  namainput.Text:='';
  //jabatancombo.items.Add('Pilih Jabatan');
  //posisicombo.Items.Add('Pilih Posisi');
  kabupatencombo.Items.Add('Pilih Kab/Kota');
  Kecamatancombo.Items.Add('Pilih Kecamatan');

  if(slogin='00')then posisicombo.Items.Add('KPU Prov');    //1
  if(length(slogin) = 2) then posisicombo.Items.Add('KPU Kab/Kot'); //2
  posisicombo.Items.Add('PPK');         //3
  posisicombo.Items.Add('PPS');         //4
  posisicombo.ItemIndex:=0;
  Jabatancombo.Items.Add('Ketua');
  Jabatancombo.Items.Add('Anggota');
  Jabatancombo.ItemIndex:=0;

  kabupatencombo.Enabled:=false;
  kecamatancombo.Enabled:=false;
  desacombo.Enabled:=false;

  ZTable1.Active := False;
  ZTable1.TableName := 'kabkot';
  if(slogin='00')then
  begin
    ZTable1.Active := True;
    ZTable1.First;
    while (not ZTable1.Eof) do
    begin
      kabupatencombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
      ZTable1.Next;
    end;
  end
  else if(Length(slogin) = 2)then
  begin
    ztable1.Filter:='KODE='+quotedstr(slogin);
    ztable1.Filtered:=true;
    ZTable1.Active := True;
    ZTable1.First;
    skota:=slogin;
    kabupatencombo.Items.add(ztable1.fieldbyname('TAMPIL').asstring);
    kabupatencombo.enabled:=false;
    kabupatencombo.ItemIndex:=1;

    ZTable1.Active := false;
    ZTable1.tablename := 'kecamatan';
    ztable1.Filter:='KODE_KABKOT='+quotedstr(slogin);
    ztable1.Filtered:=true;
    ZTable1.Active := True;
    ZTable1.First;
    while (not ztable1.Eof)do
    begin
      kecamatancombo.Items.Add(ztable1.fieldbyname('TAMPIL').asstring);
      ztable1.next;
    end;
  end
  else
  begin
    kkota := copy(slogin, 1, 2);
    kkec := slogin;
    ZTable1.filter:='KODE='+quotedstr(kkota);
    ZTable1.filtered:=true;
    ZTable1.Active := True;
    ZTable1.First;
    skota:=kkota;
    //textkota:=slogin;
    skec := slogin;
    kabupatencombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    kabupatencombo.enabled:=false;
    kabupatencombo.ItemIndex:=1;

    ZTable1.Active := false;
    ZTable1.tablename:='kecamatan';
    ztable1.filter:='KODE_KECAMATAN='+quotedstr(slogin);
    ztable1.filtered:=true;
    ZTable1.active:=true;
    ZTable1.first;
    kecamatancombo.Items.Add(ztable1.fieldbyname('TAMPIL').asstring);
    kecamatancombo.enabled:=false;
    kecamatancombo.itemindex := 1;

    ZTable1.Active := false;
    ZTable1.tablename:='desa';
    ztable1.filter:='KODE_KECAMATAN='+quotedstr(slogin);
    ztable1.filtered:=true;
    ZTable1.active:=true;
    ZTable1.first;
    while(not ZTable1.eof)do
    begin
      desacombo.Items.Add(ztable1.fieldbyname('TAMPIL').asstring);
      ZTable1.next;
    end;

  end;
end;


procedure TPPS_Form.kabupatencomboChange(Sender: TObject);
var
  pilihan:String;
begin
  if (kabupatencombo.ItemIndex > 0) then
  begin
    pilihan := kabupatencombo.Items[kabupatencombo.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        skota := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(skota);
        Filtered := True;
        Active := True;
        First;
      end;
    kecamatancombo.Clear;
    kecamatancombo.Items.Add('PILIH KECAMATAN');
    kecamatancombo.ItemIndex:=0;
    desacombo.Clear;
    desacombo.Items.Add('PILIH KELURAHAN/DESA');
    desacombo.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        kecamatancombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    skota:='';
    kecamatancombo.Clear;
    desacombo.Clear;
  end;
  sKec:='';
  sDesa:='';
end;

procedure TPPS_Form.kecamatancomboChange(Sender: TObject);
var
  pilihan:String;
begin
  if (kecamatancombo.ItemIndex > 0) then
  begin
    pilihan := kecamatancombo.Items[kecamatancombo.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+ ' and KODE_KABKOT = ' + QuotedStr
      (skota);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    desacombo.Clear;
    desacombo.Items.Add('PILIH KELURAHAN/DESA');
    desacombo.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        desacombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKec:='';
    desacombo.Clear;
  end;
  sDesa:='';
end;
procedure TPPS_Form.posisicomboChange(Sender: TObject);
var pilihan:integer;
begin
  pilihan:=posisicombo.ItemIndex;
  if((slogin<>'00') AND (length(slogin) = 2))then pilihan:=pilihan+1
  else if(length(slogin) > 2)then pilihan:=pilihan+2;
  if(pilihan=0)then
  begin
    kabupatencombo.Enabled:=false;
    kecamatancombo.Enabled:=false;
    desacombo.Enabled:=false;
  end
  else if(pilihan=1)then
  begin
    if(slogin='00')then kabupatencombo.Enabled:=true;
    kecamatancombo.Enabled:=false;
    desacombo.Enabled:=false;
  end
  else if(pilihan=2)then
  begin
    if(slogin='00')then kabupatencombo.Enabled:=true;
    if(length(slogin) = 2) then kecamatancombo.Enabled:=true;
    desacombo.Enabled:=false;
  end
  else if(pilihan=3)then
  begin
    if(slogin='00')then kabupatencombo.Enabled:=true;
    if(length(slogin) = 2) then kecamatancombo.Enabled:=true;
    desacombo.Enabled:=true;
  end;
end;

procedure TPPS_Form.tambahbtnClick(Sender: TObject);
var posisi:string;
    pilihan:integer;
    kodekabkot,kodekec,kodedes:string;
    bisa:boolean;
begin
  if(namainput.Text <> '') then begin
  pilihan:=posisicombo.ItemIndex;
  if(slogin<>'00')then pilihan:=pilihan+1;
  if(length(slogin) > 2) then pilihan := pilihan+1;
  if(jabatancombo.itemIndex=0)then
  begin
    //kabkot harus ada kotanya, PPK harus ada kec nya, PPS harus ada DESA nya
    bisa := cekMasukan(pilihan);
    if(bisa = true) then begin
    with ztable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='KODE_KABKOT='+QuotedStr(skota)+' and KODE_KECAMATAN='+QuotedStr(skec)+' and KODE_DESA='+QuotedStr(sdesa)+' and JABATAN='+QuotedStr('1')+' and POSISI='+quotedstr(inttostr(pilihan+1));
      filtered:=true;
      active:=true;
      first;
      if(RecordCount>=1)then
      begin
        ShowMessage('Jabatan ketua sudah dipegang orang lain');
      end
      else
      begin
        with ZQuery1 do
        begin
          active:=false;
          sql.Clear;
          sql.Text:='insert into petugas (NAMA,POSISI,KODE_KABKOT,KODE_KECAMATAN,KODE_DESA,JABATAN) '+
          'values('+QuotedStr(namainput.Text)+','+QuotedStr(IntToStr(pilihan+1))+','+QuotedStr(skota)+','+QuotedStr(skec)+','+QuotedStr(sdesa)+','+QuotedStr(IntToStr(jabatancombo.ItemIndex+1))+')';
          ExecSQL;
        end;
        ShowMessage('Data telah berhasil dimasukkan');
        PostMessage(Self.Handle,wm_close,0,0);
      end;
    end;
    end;
  end
  else
  begin
    bisa := cekMasukan(pilihan);
    if(bisa = true) then begin
    with ztable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='KODE_KABKOT='+QuotedStr(skota)+' and KODE_KECAMATAN='+QuotedStr(skec)+' and KODE_DESA='+QuotedStr(sdesa)+' and JABATAN='+QuotedStr('2');
      filtered:=true;
      active:=true;
      first;
      if((RecordCount>=4) and(pilihan<3))then
      begin
        ShowMessage('Jabatan anggota sudah berjumlah 4 orang');
      end
      else if((RecordCount>=2)and(pilihan=3))then
      begin
        ShowMessage('Jabatan anggota sudah berjumlah 2 orang');
      end
      else
      begin
        with ZQuery1 do
        begin
          active:=false;
          sql.Clear;
          sql.Text:='insert into petugas (NAMA,POSISI,KODE_KABKOT,KODE_KECAMATAN,KODE_DESA,JABATAN) '+
          'values('+QuotedStr(namainput.Text)+','+QuotedStr(IntToStr(pilihan+1))+','+QuotedStr(skota)+','+QuotedStr(skec)+','+QuotedStr(sdesa)+','+QuotedStr(IntToStr(jabatancombo.ItemIndex+1))+')';
          ExecSQL;

        end;
        ShowMessage('Data telah berhasil dimasukkan');
        PostMessage(Self.Handle,wm_close,0,0);
      end;
    end;
  end;
  end;
  end
  else
    showmessage('Nama petugas harus diisi');
end;

function TPPS_Form.cekmasukan(posisi:integer):boolean;
var
  retval : boolean;
begin
  retval := true;
  if(posisi > 0) then begin
    if(skota = '') then begin
      showMessage('Kabupaten/Kota belum diisi, silakan periksa kembali');
      retval := false;
    end
    else if(skec = '') then begin
      if(posisi > 1) then begin
        showMessage('Kecamatan belum diisi, silakan periksa kembali');
        retval := false;
      end;
    end
    else if (sdesa = '') then begin
      if(posisi > 2) then begin
        showMessage('Kelurahan/Desa belum diisi, silakan periksa kembali');
        retval := false;
      end;
    end;
  end;
  Result := retval;
end;

end.
